<template>
  <div class="coursesList">
    <div v-if="!getCoursesData.length" style="margin-top: 20px">
      暂无他人课程！
    </div>
    <div
      class="course hover_boxShadow_defaut"
      :class="{ latelyOpen: item.latelyOpen }"
      v-for="(item, index) in getCoursesData"
      :key="index"
      :style="{
        'margin-right': (index + 1) % 4 === 0 ? '0' : '20px',
      }"
      @click="handelJumpRoute(item)"
      :title="item.name"
    >
      <div class="continueLabel" v-if="item.isContinuousPublish">
        <span class="content">持续发布</span>
      </div>
      <div class="containerBox" v-if="!item.createCourses">
        <div class="iconBox">
          <img
            v-lazy=" item.iconPath"
            :title="item.iconPath ? '课程封面' : '暂无课程封面'"
          />
        </div>
        <div class="courseName">
          {{ item.name || "【缺课程名称】" }}
        </div>
        <div class="courseDesc">设计：{{ filterUserName(item.mainDesignerUserID) }}</div>
        <div class="courseInfo">
          <div class="desc">简介：{{ item.description || "无" }}</div>
        </div>
        <div class="time">状态：{{ item.state }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { getDesigncourselist } from "@/request/designcourse";
import { mapState } from "vuex";
import { location, renderTimeToYMD, currTime } from "@/util/tool";

export default {
  data() {
    return {
      latelyOpen: [],
      coursesData: [],
      location: location(window),
    };
  },
  filters: {
    filterTime(val) {
      const time = new Date(val);
      return renderTimeToYMD(time) + ' ' + currTime(time);
    },
  },
  created() {
    if (!this.userList.length) this.$store.dispatch("user/getUserList")
    this.fetchCurriculumlist();
  },
  computed: {
    ...mapState({
      userInfo: (state) => state.user.userInfo,
      userList:(state) => state.user.userList,
      otherDesigncourselist: (state) => state.courses.otherDesigncourselist
    }),
    getCoursesData() {
      var nData = JSON.parse(JSON.stringify(this.otherDesigncourselist));
      nData.sort((a, b) => {
        let t_1 = new Date(a.lastUpdatedTime).getTime();
        let t_2 = new Date(b.lastUpdatedTime).getTime();
        return t_2 - t_1;
      });
      return nData;
    },
    filterUserName: () => {
      return function (val) {
        const findUser = this.userList.find(item => item.userID === val)
        return !findUser ? val : findUser.realName || findUser.nickName || findUser.email || findUser.regName;
      }
    }
  },
  methods: {
    // 获取他人设计的课程
    fetchCurriculumlist() {
      this.$store.dispatch("courses/fetchDesignCourses", {that: this})
      // getDesigncourselist().then((data) => {
      //   data = data?.filter((item) => {
      //     return [43, 44, 48, 32, 38, 19].indexOf(item.courseID) == -1;
      //   });
      //   // 过滤他人的课程 一般是后端过滤
      //   data = data?.filter(
      //     (item) => item.mainDesignerUserID !== this.userInfo.userID
      //   );
      //   //  近期的排在前面
      //   data.sort((a, b) => {
      //     let t_1 = new Date(a.lastUpdatedTime).getTime();
      //     let t_2 = new Date(b.lastUpdatedTime).getTime();
      //     return t_2 - t_1;
      //   });

      //   // data.map((item, index) => {
      //   //   if (index <= 6) {
      //   //     this.$set(item, "latelyOpen", true);
      //   //   }
      //   // });
      //   this.coursesData = data;
      // });
    },
    // 跳转课程创作页面
    handelJumpRoute(item) {
      if (item.courseID) {
        this.$router.push({
          name: "Creativeourse",
          query: {
            courseID: item.courseID,
          },
          params: { id: item.courseID }
        });
      } else {
        this.$router.push({
          name: "Creativeourse",
        });
      }
    },
  },
};
</script>

<style lang="stylus">
$boderColor = solid 1px #b7b2b2;

.coursesList {
  // padding-top: 20px;
  margin: 0 auto;
  max-width: 1200px;
  display: flex;
  justify-content: flex-start;
  flex-flow: row wrap;

  .course {
    cursor: pointer;
    position: relative;
    margin-right: 36px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-top: 25px;
    width: 285px;
    height: 340px;
    background-color: #ffffff;
    // box-shadow: 0px 8px 16px 0px rgba(79, 111, 139, 0.16);
    border-radius: 5px;
    overflow: hidden;

    .addCourse {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;

      .title {
        margin-top: 64px;
        font-family: PMingLiU-ExtB;
        font-size: 22px;
        color: rgba(129, 129, 129, 0.63);
      }
    }

    .containerBox {
      padding: 17px 15px 0;

      .iconBox {
        width: 255px;
        height: 146px;

        img {
          object-fit: cover;
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }
      }

      .courseName {
        margin-top: 7px;
        display: inline-block;
        line-height: 27px;
        font-family: PMingLiU-ExtB;
        font-weight: bold;
        font-size: 20px;
        color: #333333;
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: break-all;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }

      .courseDesc {
        margin-top: 5px;
        line-height: 19px;
        font-family: PMingLiU-ExtB;
        font-size: 14px;
        color: #999999;
      }

      .courseInfo {
        margin-top: 9px;
        line-height: 19px;
        display: flex;
        font-family: PMingLiU-ExtB;
        font-size: 14px;
        color: #666666;

        .desc {
          margin: 0 10px;
          position: relative;
          // height: 57px;
          overflow: hidden;
          text-overflow: ellipsis;
          word-break: break-all;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }

        .desc:first-child {
          margin-left: 0;
        }

        .desc:last-child {
          margin-right: 0;
        }

        .desc::after {
          position: absolute;
          content: '';
          top: 1.5px;
          right: -10px;
          height: 15px;
          width: 1px;
          background: #666666;
        }

        .desc:last-child::after {
          content: none;
        }
      }

      .time {
        margin-top: 9px;
        line-height: 19px;
        font-family: PMingLiU-ExtB;
        font-size: 14px;
        color: #666666;
      }
    }

    .continueLabel {
      top: -15px;
      right: 0px;
      width: 145px;
      height: 145px;
      position: absolute;
      overflow: hidden;

      .content {
        top: 25px;
        right: -70px;
        transform: rotate(45deg);
        display: inline-block;
        text-align: center;
        width: 200px;
        height: 40px;
        line-height: 40px;
        position: absolute;
        z-index: 2;
        color: #fff;
        overflow: hidden;
        box-shadow: 0 0 1px 1px #035386;
        background: #035386;
      }
    }
  }

  .latelyOpen {
    position: relative;

    .latelyIconBox {
      position: absolute;
      right: 5px;
      top: 5px;

      img {
        width: 20px;
        height: 20px;
      }
    }
  }
}
</style>
